package cn.ecnu.user.mapper;

import cn.ecnu.user.pojo.User;
import cn.ecnu.user.vo.AdminUserVo;
import cn.ecnu.user.vo.UserInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM `user` WHERE user_id = #{uid} AND flag = 1")
    User getUserById(@Param("id") Integer uid);


    AdminUserVo getUserVoById(@Param("uid") Integer uid);

    @Select("SELECT * FROM `user` WHERE phone = #{phone} AND flag = 1")
    User getUserByPhone(@Param("phone") String phone);

    int addUser(User user);

    @Select("Select * FROM `user` WHERE alipay_id = #{alipayId} AND flag = 1")
    User getUserByAlipayId(@Param("alipayId") String alipayId);

    int updateUserById(User user);

    @Update("UPDATE `user` SET flag = 0 WHERE user_id = #{uid}")
    int deleteUserById(@Param("uid") Integer uid);


    @Update("UPDATE `user` SET `role` = #{rid} WHERE user_id = #{uid}")
    int changeUserRole(@Param("uid") Integer userId, @Param("rid") Integer roleId);

    @Update("UPDATE `user` SET `role` = 2 WHERE user_id = #{uid}")
    int setUserAsAdminByUid(@Param("uid") Integer uid);

    // 表示查询开始的索引, 总共有pageSize个
    @Select("SELECT * FROM `user` WHERE flag = 1 limit #{startNum}, #{pageSize}")
    List<User> getUserListUsePage(@Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);


    List<AdminUserVo> getAdminUserVo(@Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("SELECT COUNT(*) FROM `user`")
    Integer getTotal();

    List<AdminUserVo> getAdminUserVoAll();

    @Update("UPDATE `user` SET user_name = #{userName}, alipay_id = #{alipayId}, phone = #{phone}, email = #{email}, avatar = #{avatar} WHERE user_id = #{userId}")
    int updateUserVoById(UserInfoVo vo);
}
